const path = require("path");

const {CleanWebpackPlugin} = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const {DefinPlugin} = require('webpack');

module.exports = {
    entry: "./src/main.js",
    output: {
        path: path.resolve(__dirname, "./build"),
        filename: "js/bundle.js"
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: [
                    "style-loader",
                    "css-loader",
                    "postcss-loader"
                ]
            },
            {
                test: /\.less$/,
                use: [
                    "style-loader",
                    "css-loader",
                    "less-loader"
                ]
            },
            {
                test: /\.(jpe?g|png)$/,
                type: 'asset',
                generator: {
                    filename: "img/[name]_[hash:2][ext]"
                },
                parser: {
                    dataUrlCondition: {
                        maxSize: 100 * 1024
                    }
                }
            },
            //打包字体2
            {
                test: /\.(woff2?|ttf|eot)$/,
                type: "asset/resource",
                generator: {
                    filename: "font/[name]_[hash:2][ext]"
                }
            },
            {
                test: /\.js$/,
                use: {
                    loader: "babel-loader",
                    options: {
                        //1.es6转es5
                        // plugins: [
                        //     "@babel/plugin-transform-arrow-functions",
                        //     "@babel/plugin-transform-block-scoping"
                        // ]
                        //2.es6转es5
                        presets: [
                            "@babel/preset-env"
                        ]
                    }
                }
            }
        ]
    },

    //打包自动删除build(dist)文件夹
    plugins: [
        // new CleanWebpackPlugin(),

        // //dist生成html文件
        // new HtmlWebpackPlugin()

        //制定模板打包
        // new HtmlWebpackPlugin({
        //     template: "./public/index.html"
        // }),
        // new DefinPlugin({
        //     BASE_URL: "'./'"
        // })
    ]
}